<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ph="http://java.sun.com/jsf/composite/pharmacy">

    <ui:define name="content">
        <h:form>
            <h:panelGroup rendered="#{!webUserController.hasPrivilege('PharmacyOrderCreation')}" >
                You are NOT authorized
            </h:panelGroup>
            <p:panel rendered="#{webUserController.hasPrivilege('PharmacyOrderCreation')}" >
                <f:facet name="header">

                    <p:outputLabel value="Purchase order Request Order"/>
                    <p:commandButton ajax="false" value="Request" action="#{purchaseOrderRequestController.request}"/>
                    <p:commandButton ajax="false"  value="New Order" action="#{purchaseOrderRequestController.recreate}"/>
                </f:facet>

                <p:tabView>
                    <p:tab title=" Distributor">                                               
                        <p:autoComplete converter="deal" value="#{purchaseOrderRequestController.currentBill.toInstitution}"  
                                        forceSelection="true"
                                        completeMethod="#{dealerController.completeDealor}"
                                        var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <f:ajax event="itemSelect" execute="@this" render=":#{p:component('itemList')}" listener="#{purchaseOrderRequestController.setInsListener}"/>
                        </p:autoComplete>
                        <h:panelGroup>                            
                            <p:commandButton ajax="false" value="Fill with Item" action="#{purchaseOrderRequestController.createOrderWithItems}" disabled="#{purchaseOrderRequestController.currentBill.id ne null}"/>
                        </h:panelGroup>
                    </p:tab>

                    <p:tab title="Add Dealors Item ">
                        <p:autoComplete id="exDItem" value="#{purchaseOrderRequestController.currentBillItem.item}"
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeDealorItem}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" >
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" >
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                            <p:column headerText="Current Stock" >
                                <p:outputLabel value="#{stockController.departmentItemStock(sessionController.department , vt)}"/>                            
                            </p:column>
                        </p:autoComplete>
                        <p:commandButton value="Add Item" action="#{purchaseOrderRequestController.addItem}" process="exDItem @this" update="exDItem :#{p:component('itemList')} tot" />

                    </p:tab>

                    <p:tab title="Add Extra Item ">
                        <p:autoComplete id="exItem" value="#{purchaseOrderRequestController.currentBillItem.item}"
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeAmpItem}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" >
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" >
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                        </p:autoComplete>
                        <p:commandButton value="Add Item" action="#{purchaseOrderRequestController.addItem}" process="exItem @this" update="exItem :#{p:component('itemList')} tot" />

                    </p:tab>
                    <p:tab title="Payment Method">
                        <p:selectOneMenu   id="cmbPs" value="#{purchaseOrderRequestController.currentBill.paymentMethod}">    
                            <f:selectItem itemLabel="SelectPayment method"/>
                            <f:selectItems value="#{enumController.paymentMethodsForPo}"/>
                        </p:selectOneMenu>
                        <p:outputLabel id="tot" style="font-weight: bold;" value="#{purchaseOrderRequestController.currentBill.netTotal}">
                            <f:convertNumber pattern="#,##0.00"/>
                        </p:outputLabel>
                    </p:tab>

                </p:tabView>

                <p:dataTable styleClass="noBorder" rowIndexVar="i" var="bi" scrollable="true"
                             rowKey="#{bi.searialNo}"
                             scrollHeight="250" value="#{purchaseOrderRequestController.billItems}" 
                             selection="#{purchaseOrderRequestController.selectedBillItems}"
                             id="itemList" >  

                    <f:facet name="header">  
                        <h:outputLabel  value="Pharmacy Bill Item"/>
                        <p:commandButton ajax="false" value="Remove All" 
                                         action="#{purchaseOrderRequestController.removeSelected()}"/>
                    </f:facet>  

                    <p:column selectionMode="multiple" style="width: 30px;"/>


                    <p:column headerText="No" style="width: 50px;" >
                        #{bi.searialNo+1}
                    </p:column>

                    <p:column headerText="Item Name"  >  
                        #{bi.item.name} 
                    </p:column>

                    <p:column headerText="Code" styleClass="shortNumericColumn"  >  
                        #{bi.item.code} 
                    </p:column>

                    <p:column headerText="Qty" styleClass="averageNumericColumn" > 
                        <p:inputText autocomplete="off"  id="qty" value="#{bi.tmpQty}" style="width:100%" label="Qty">  
                            <f:convertNumber pattern="#00.00"/>
                            <f:ajax event="blur" render="total :#{p:component('tot')} "  execute="@this price" listener="#{purchaseOrderRequestController.onEdit(bi)}" ></f:ajax>
                            <f:ajax event="focus" render=":#{p:component('tab')}" listener="#{purchaseOrderRequestController.onFocus(bi)}" />
                        </p:inputText>
                    </p:column> 

                    <p:column headerText="Free Qty" styleClass="averageNumericColumn" > 
                        <p:inputText autocomplete="off"  id="freeQty" value="#{bi.pharmaceuticalBillItem.freeQty}" style="width:100%" label="Qty">  
                            <f:convertNumber pattern="#00.00"/>
                            <f:ajax event="blur" render="total :#{p:component('tot')} "  execute="@this price" listener="#{purchaseOrderRequestController.onEdit(bi)}" ></f:ajax>
                            <f:ajax event="focus" render=":#{p:component('tab')}" listener="#{purchaseOrderRequestController.onFocus(bi)}" />
                        </p:inputText>
                    </p:column>

                    <p:column headerText="Purchase Price" styleClass="averageNumericColumn" >  
                        <h:panelGroup id="price">
                            <p:inputText autocomplete="off" value="#{bi.pharmaceuticalBillItem.purchaseRate}">
                                <f:convertNumber pattern="#00.00"/>
                                <f:ajax event="blur" render="total :#{p:component('tot')}"  execute="@this qty" listener="#{purchaseOrderRequestController.onEdit(bi)}" ></f:ajax>
                                <f:ajax event="focus" render=":#{p:component('tab')}" listener="#{purchaseOrderRequestController.onFocus(bi)}" />
                            </p:inputText>
                        </h:panelGroup>
                    </p:column>  

                    <p:column headerText="Total" styleClass="averageNumericColumn" >  
                        <h:panelGroup id="total">
                            <h:outputText value="#{bi.pharmaceuticalBillItem.purchaseRate*bi.pharmaceuticalBillItem.qty}" >
                                <f:convertNumber pattern="#,##0.00"/>
                            </h:outputText>
                        </h:panelGroup>
                    </p:column>  

                    <p:column styleClass="averageNumericColumn" >
                        <p:commandButton value="Remove" ajax="false" action="#{purchaseOrderRequestController.removeItem(bi)}"/>
                    </p:column>

                </p:dataTable>  
                <p:spacer height="50"/>
                <ph:history/>
            </p:panel>            
        </h:form>
    </ui:define>  

</ui:composition>
